<?php
/**
 * 
 * 
 * @author Elwin
 *
 */
class CArrayTree
{
	/**
	 * @param array $array
	 * @param string $findValue
	 * @param array $path
	 * @return array:
	 */
	public function findValueArray(array $array, $findValue, array $path = array())
	{
		$result = array ();
		static $founder = array ();
		foreach ( $array as $key => $val )
		{
			$currentPath = array_merge ( $path, array (
					$key
			) );
			if (is_array ( $val ))
			{
				$result = array_merge ( $result, self::findValueArray ( $val, $findValue, $currentPath ) );
			} else
			{
				$result = $currentPath;
				if ($findValue == $val)
					$founder = $result;
			}
		}
		return $founder;
	}
	
	/*
	 * $arr = array( 'val1' => 1, 'val2' => 2, 'val3' => array( 'val3.1' => 1,
	 * 'val3.2' => array( 'val3.2.1' => 'special' ), 'val3.3' => 3 ), 'val4' =>
	 * 4 ); var_dump(pa($arr)); 
	 * 
	 * // string 'val3->val3.2->val3.2.1' (length=22)
	 */
	public static function path($arr, $val = 'special', $stack = array())
	{
		foreach ( $arr as $k => $v )
		{
			if (is_array ( $v ))
			{
				array_push ( $stack, $k );
				return self::pa ( $v, $val, $stack );
			}
			if ($v == $val)
			{
				array_push ( $stack, $k );
				return implode ( '/', $stack );
			}
		}
		return false;
	}
	
	/**
	 * @example
	 * $path = 'BLG/DomainPaths/Pic1';
	 * $path_parts = explode('/', $path);
	 * print_r(pathToArray($path_parts));
	 * 
	 * @param string $input
	 * @param unknown_type $endvalue
	 * @param unknown_type $res
	 * @return unknown
	 */
	public static function pathToArray($input, $endvalue = 1, $res = array())
	{
		if ($element = array_shift($input))
		{
			$local_result = self::pathToArray($input, $endvalue, $res);
			$res[$element] = empty($local_result) ? $endvalue : $local_result;
		}
		return $res;
	}
}
?>